import { defineStore } from 'pinia';
import { ref } from 'vue';
import {
  CREATE_PAGE_RECORD,
  CREATE_USER,
  DELETE_PAGE_RECORD_BY_ID,
  DELETE_USER_BY_ID,
  EDIT_PAGE_RECORD,
  EDIT_USER,
  POST_PAGE_LIST_DATA,
  POST_USER_LIST_DATA
} from '@/api/main/system';
import type { ICreateUser, IQueryInfo, IUser } from '@/types/system';
import { ElMessage } from 'element-plus';
import { useMainStore } from '@/store/mian';

export const useSystemStore = defineStore('system', () => {
  const stateUserList = ref<IUser[]>();
  const stateTotalCount = ref<string>();

  async function actionPostUserList(queryInfo: IQueryInfo) {
    try {
      const userListRes = await POST_USER_LIST_DATA(queryInfo);
      const { list, totalCount } = userListRes;
      stateTotalCount.value = totalCount;
      stateUserList.value = list;
    } catch (err) {
      console.warn(err);
    }
  }

  async function actionDeleteUserById(id: number) {
    try {
      await DELETE_USER_BY_ID(id);
    } catch (err) {
      console.warn(err);
    }
  }

  async function actionCreateUser(userInfo: ICreateUser) {
    try {
      // 创建用户
      const res = await CREATE_USER(userInfo);
      console.log(res);
    } catch (err) {
      console.warn(err);
    }
  }

  async function actionEditUser(id: number, userInfo: any) {
    try {
      // 编辑用户
      const res = await EDIT_USER(id, userInfo);
      console.log(res);
    } catch (err) {
      console.warn(err);
    }
  }

  const statePageList = ref<any>();
  const statePageTotalCount = ref<number>();

  /**
   * @function fetchPageListDataAction 获取页面列表数据
   * @param pageName 页面名称
   * @param queryInfo 查询参数
   */
  async function fetchPageListDataAction(pageName: string, queryInfo: any) {
    try {
      const pageListRes = await POST_PAGE_LIST_DATA(pageName, queryInfo);
      const { list, totalCount } = pageListRes;
      statePageTotalCount.value = totalCount;
      statePageList.value = list;
    } catch (err) {
      console.warn(err);
    }
  }

  /**
   * @function deleteRecordByIdAction 删除一条数据
   * @param pageName 页面名称
   * @param id 当前数据id
   */
  async function deleteRecordByIdAction(pageName: string, id: number) {
    try {
      await DELETE_PAGE_RECORD_BY_ID(pageName, id);
      ElMessage.success('删除成功');
      await fetchPageListDataAction(pageName, { offset: 0, size: 10 });
      if (pageName !== 'users') {
        const mainStore = useMainStore();
        await mainStore.actionFetchEntireData();
      }
    } catch (err) {
      console.warn(err);
    }
  }

  /**
   * @function createRecordAction 创建一条数据记录
   * @param pageName
   * @param record
   */
  async function createRecordAction(pageName: string, record: any) {
    try {
      await CREATE_PAGE_RECORD(pageName, record);
      ElMessage.success('创建成功');
      await fetchPageListDataAction(pageName, { offset: 0, size: 10 });
      if (pageName !== 'users') {
        const mainStore = useMainStore();
        await mainStore.actionFetchEntireData();
      }
    } catch (err) {
      console.warn(err);
    }
  }

  /**
   * @function editRecordByIdAction 编辑一条数据记录
   * @param pageName 页面名称
   * @param id 当前数据id
   * @param record 修改后的数据对象
   */
  async function editRecordByIdAction(pageName: string, id: number, record: any) {
    try {
      await EDIT_PAGE_RECORD(pageName, id, record);
      ElMessage.success('编辑成功');
      await fetchPageListDataAction(pageName, { offset: 0, size: 10 });
      if (pageName !== 'users') {
        const mainStore = useMainStore();
        await mainStore.actionFetchEntireData();
      }
    } catch (err) {
      console.warn(err);
    }
  }

  return {
    stateUserList,
    stateTotalCount,
    actionPostUserList,
    actionDeleteUserById,
    actionCreateUser,
    actionEditUser,
    statePageList,
    statePageTotalCount,
    fetchPageListDataAction,
    deleteRecordByIdAction,
    createRecordAction,
    editRecordByIdAction
  };
});
